<?php

namespace App\Http\Controllers\home;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\PaymentController;
use App\Models\Activitys;
use App\Models\Addresses;
use App\Models\Appointments;
use App\Models\Areas;
use App\Models\Articles;
use App\Models\BalanceLogs;
use App\Models\BidLists;
use App\Models\BondReturnLogs;
use App\Models\Buys;
use App\Models\Cases;
use App\Models\CashOuts;
use App\Models\Categorys;
use App\Models\Collections;
use App\Models\CompanyInfos;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Feedbacks;
use App\Models\Footprints;
use App\Models\Goods;
use App\Models\GoodsCategorys;
use App\Models\GoodsEvaluates;
use App\Models\IntegralLogs;
use App\Models\Mails;
use App\Models\Messages;
use App\Models\OrderGoods;
use App\Models\Orders;
use App\Models\ProfitLogs;
use App\Models\Regions;
use App\Models\Services;
use App\Models\SignUps;
use App\Models\Stores;
use App\Models\TicketsList;
use App\Models\UserInfos;
use App\Models\Users;
use App\Models\UsersAddress;
use App\Models\UsersLog;
use App\Models\UsersRank;
use App\Models\Complaints;
use App\Models\WordDownloadLogs;
use App\Models\Words;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use test\Mockery\Fixtures\MethodWithVoidReturnType;

class MyController extends Controller
{
    /**
     * HR个人中心-首页页面
     * @param url home.hrmy.hrIndex
     */
    public function hrIndex(){
        return view('home.hrmy.index');
    }
    /**
     * HR个人中心-基本信息页面
     * @param url home.hrmy.hrInfo
     */
    public function hrInfo(){
        return view('home.hrmy.info');
    }
    /**
     * HR个人中心-基本信息修改页面
     * @param url home.hrmy.hrInfoEdit
     */
    public function hrInfoEdit(){
        return view('home.hrmy.info_edit');
    }
    /**
     * HR个人中心-安全中心页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafe(){
        return view('home.hrmy.safe');
    }
    /**
     * HR个人中心-安全中心-密码修改页面
     * @param url home.hrmy.hrSafePass
     */
    public function hrSafePass(){
        return view('home.hrmy.safe_pass');
    }
    /**
     * HR个人中心-安全中心-手机号修改页面
     * @param url home.hrmy.hrSafePhone
     */
    public function hrSafePhone(){
        return view('home.hrmy.safe_phone');
    }
    /**
     * HR个人中心-安全中心-微信绑定页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafeWeixin(){
        return view('home.hrmy.safe_weixin');
    }
    /**
     * HR个人中心-安全中心-QQ绑定页面
     * @param url home.hrmy.hrSafe
     */
    public function hrSafeQQ(){
        return view('home.hrmy.safe_qq');
    }
    /**
     * HR个人中心-收货地址
     * @param url home.hrmy.hrAddress
     */
    public function hrAddress(){
        return view('home.hrmy.address');
    }
    /**
     * HR个人中心-收货地址-新增
     * @param url home.hrmy.hrAddressAdd
     */
    public function hrAddressAdd(){
        return view('home.hrmy.address_add');
    }
    /**
     * HR个人中心-提现
     * @param url home.hrmy.hrCashOut
     */
    public function hrCashOut(){
        return view('home.hrmy.cash_out');
    }
    /**
     * HR个人中心-资产管理中心
     * @param url home.hrmy.hrAssets
     */
    public function hrAssets(){
        return view('home.hrmy.assets');
    }
    /**
     * HR个人中心-资产管理中心-消费记录
     * @param url home.hrmy.hrAssetsConsume
     */
    public function hrAssetsConsume(){
        return view('home.hrmy.assets_consume');
    }
    /**
     * HR个人中心-订单管理中心
     * @param url home.hrmy.hrOrder
     */
    public function hrOrder(){
        return view('home.hrmy.order');
    }
    /**
     * HR个人中心-订单管理中心
     * @param url home.hrmy.hrOrderDetails
     */
    public function hrOrderDetails(){
        return view('home.hrmy.order_details');
    }
    /**
     * HR个人中心-需求管理中心
     * @param url home.hrmy.hrDemand
     */
    public function hrDemand(){
        return view('home.hrmy.demand');
    }
    /**
     * HR个人中心-收藏-店铺
     * @param url home.hrmy.hrCollectStore
     */
    public function hrCollectStore(){
        return view('home.hrmy.collect_store');
    }
    /**
     * HR个人中心-收藏-商品
     * @param url home.hrmy.hrCollectGoods
     */
    public function hrCollectGoods(){
        return view('home.hrmy.collect_goods');
    }
    /**
     * HR个人中心-收藏-文档
     * @param url home.hrmy.hrCollectWord
     */
    public function hrCollectWord(){
        return view('home.hrmy.collect_Word');
    }
    /**
     * HR个人中心-收藏-新闻
     * @param url home.hrmy.hrCollectNews
     */
    public function hrCollectNews(){
        return view('home.hrmy.collect_news');
    }
    /**
     * HR个人中心-收藏-活动
     * @param url home.hrmy.hrCollectAct
     */
    public function hrCollectAct(){
        return view('home.hrmy.collect_act');
    }
    /**
     * HR个人中心-下载记录-文档
     * @param url home.hrmy.hrDownloadWord
     */
    public function hrDownloadWord(){
        return view('home.hrmy.download_word');
    }
    /**
     * HR个人中心-活动管理中心
     * @param url home.hrmy.hrActivity
     */
    public function hrActivity(){
        return view('home.hrmy.activity');
    }
    /**
     * HR个人中心-足迹
     * @param url home.hrmy.hrFootprint
     */
    public function hrFootprint(){
        return view('home.hrmy.footprint');
    }
    /**
     * HR个人中心-消息查看中心
     * @param url home.hrmy.hrMail
     */
    public function hrMail(){
        return view('home.hrmy.mail');
    }
    /**
     * HR个人中心-投诉建议列表
     * @param url home.hrmy.hrFeedBack
     */
    public function hrFeedBack(){
        return view('home.hrmy.feedback');
    }
    /**
     * HR个人中心-投诉建议-编写建议
     * @param url home.hrmy.hrFeedBackAdd
     */
    public function hrFeedBackAdd(){
        return view('home.hrmy.feedback_add');
    }
    /**
     * HR个人中心-投诉建议-详情
     * @param url home.hrmy.hrFeedBackDetails
     */
    public function hrFeedBackDetails(){
        return view('home.hrmy.feedback_details');
    }
    /**
     * HR个人中心-平台客服
     * @param url home.hrmy.hrCustomerService
     */
    public function hrCustomerService(){
        return view('home.hrmy.customer_service');
    }


    /**
     * 供应商个人中心-审核页面
     * @param url home.suppliermy.SupplierExamine
     */
    public function supplierExamine(){
        return view('home.suppliermy.examine');
    }
    /**
     * 供应商个人中心-首页
     * @param url home.suppliermy.SupplierIndex
     */
    public function supplierIndex(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.index');
            }
        }
    }
    /**
     * 供应商个人中心-基本信息页面
     * @param url home.suppliermy.supplierInfo
     */
    public function supplierInfo(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.info');
            }
        }
    }
    /**
     * 供应商个人中心-基本信息修改页面
     * @param url home.suppliermy.supplierInfoEdit
     */
    public function supplierInfoEdit(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.info_edit');
            }
        }
    }
    /**
     * 供应商个人中心-安全中心页面
     * @param url home.suppliermy.supplierSafe
     */
    public function supplierSafe(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.safe');
            }
        }
    }
    /**
     * 供应商个人中心-安全中心-密码修改页面
     * @param url home.suppliermy.supplierSafePass
     */
    public function supplierSafePass(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.safe_pass');
            }
        }
    }
    /**
     * 供应商个人中心-安全中心-手机号修改页面
     * @param url home.suppliermy.supplierSafePhone
     */
    public function supplierSafePhone(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.safe_phone');
            }
        }
    }
    /**
     * 供应商个人中心-安全中心-微信绑定页面
     * @param url home.suppliermy.supplierSafeWeixin
     */
    public function supplierSafeWeixin(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.safe_weixin');
            }
        }
    }
    /**
     * 供应商个人中心-安全中心-QQ绑定页面
     * @param url home.suppliermy.supplierSafeQQ
     */
    public function supplierSafeQQ(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.safe_qq');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-财务信息管理
     * @param url home.suppliermy.supplierAssetsInfo
     */
    public function supplierAssetsInfo(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.assets_info');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-余额充值页面
     * @param url home.suppliermy.supplierBalanceAdd
     */
    public function supplierBalanceAdd(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.balance_add');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-余额充值订单页面
     * @param url home.suppliermy.supplierBalanceAddOrder
     */
    public function supplierBalanceAddOrder(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.balance_add_order');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-余额充值完成页面
     * @param url home.suppliermy.supplierBalanceAddFinish
     */
    public function supplierBalanceAddFinish(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.balance_add_finish');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-收益明细
     * @param url home.suppliermy.supplierProfit
     */
    public function supplierProfit(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.profit');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-积分消费记录页面
     * @param url home.suppliermy.supplierIntegralLogs
     */
    public function supplierIntegralLogs(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.integral_logs');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-余额消费记录页面
     * @param url home.suppliermy.supplierBalanceLogs
     */
    public function supplierBalanceLogs(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.profit');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-提现
     * @param url home.suppliermy.supplierCashOut
     */
    public function supplierCashOut(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.cash_out');
            }
        }
    }
    /**
     * 供应商个人中心-财务中心-申请退还保证金
     * @param url home.suppliermy.supplierRetreatBond
     */
    public function supplierRetreatBond(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.retreat_bond');
            }
        }
    }
    /**
     * 供应商个人中心-竞标管理中心
     * @param url home.suppliermy.supplierBidding
     */
    public function supplierBidding(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.bidding');
            }
        }
    }
    /**
     * 供应商个人中心-活动管理中心
     * @param url home.suppliermy.supplierAct
     */
    public function supplierAct(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.activity');
            }
        }
    }
    /**
     * 供应商个人中心-店铺管理中心-基本信息
     * @param url home.suppliermy.supplierStore
     */
    public function supplierStore(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.store');
            }
        }
    }
    /**
     * 供应商个人中心-店铺管理中心-修改基本信息
     * @param url home.suppliermy.supplierStore
     */
    public function supplierStoreEdit(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.store_edit');
            }
        }
    }
    /**
     * 供应商个人中心-商品管理中心
     * @param url home.suppliermy.supplierGoods
     */
    public function supplierGoods(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.goods');
            }
        }
    }
    /**
     * 供应商个人中心-发布、修改商品页面
     * @param url home.suppliermy.supplierGoodsUpdate
     * @param Request $request
     * @return bool
     */
    public function supplierGoodsUpdate(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
        return view('home.suppliermy.goods_update');
            }
        }
    }
    /**
     * 供应商个人中心-足迹
     * @param url home.suppliermy.supplierFootprint
     */
    public function supplierFootprint(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.footprint');
            }
        }
    }
    /**
     * 供应商个人中心-消息查看中心
     * @param url home.suppliermy.supplierMail
     */
    public function supplierMail(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.mail');
            }
        }
    }
    /**
     * 供应商个人中心-投诉建议列表
     * @param url home.suppliermy.supplierFeedBack
     */
    public function supplierFeedBack(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.feedback');
            }
        }
    }
    /**
     * 供应商个人中心-投诉建议-编写建议
     * @param url home.suppliermy.supplierFeedBackAdd
     */
    public function supplierFeedBackAdd(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.feedback_add');
            }
        }
    }
    /**
     * 供应商个人中心-投诉建议-详情
     * @param url home.suppliermy.supplierFeedBackDetails
     */
    public function supplierFeedBackDetails(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.feedback_details');
            }
        }
    }
    /**
     * 供应商个人中心-平台客服
     * @param url home.suppliermy.supplierCustomerService
     */
    public function supplierCustomerService(){
        $user_id = auth()->guard('web')->id();
        $userinfo = Users::where('id',$user_id)->value('status');
        if(empty($user_id) || empty($userinfo)){
            return redirect(route('home.login.login'));
        }else{
            if ($userinfo != 1){
                return redirect(route('home.suppliermy.supplierExamine'));
            }else{
                return view('home.suppliermy.customer_service');
            }
        }
    }


    /**
     * 用户签到
     * @param url home.user.userSignInApi
     * @return is_sign 是否签到：0否 1是
     */
    public function userSignInApi(){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['username','sign_in_at','type']);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            Users::where('id',$user_id)->update(['sign_in_at' => date('Y-m-d H:i:s')]);
            //当天第一次签到送积分
            if (trim(@date('Y-m-d', strtotime($userinfo['sign_in_at']))) != trim(date('Y-m-d'))) {
                $integral = Configs::getConfig('integral');
                if($userinfo['type'] == 1) {
                    $sign_in = $integral['hr_sign_in'];
                }else {
                    $sign_in = $integral['bus_login'];
                }
                Users::where('id',$user_id)->increment('integral',$sign_in);
                //添加日志记录
                $integral_arr = [
                    'user_id' => $user_id,
                    'user_name' => $userinfo['username'],
                    'integral' => '+'.$sign_in,
                    'desc' => '每日首次签到送积分',
                    'type' => 1,
                ];
                IntegralLogs::create($integral_arr);
            }
            //判断当天是否签到
            $users = Users::where('id',$user_id)->first();
            $users['is_sign'] = 0;//默认未签到
            if (trim(@date('Y-m-d', strtotime($users['sign_in_at']))) == trim(date('Y-m-d'))) {
                $users['is_sign'] = 1;//已签到
            }
            return $this->writeJson(0,$users,'签到成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户信息
     * @param url home.user.userInfoApi
     * @return is_mobile 是否绑定手机号：0否 1是
     * @return is_wechat 是否绑定微信：0否 1是
     * @return is_qq 是否绑定QQ：0否 1是
     * @return is_password 是否设置密码：0否 1是
     * @return is_sign 是否签到：0否 1是
     * @return massage 站内信未读数量
     *
     * @return (array)footprint 足迹
     * @return foot_id 浏览内容id
     * @return type 类型：1供应商 2需求 3商品 4活动 5文档
     * @return created_at 浏览时间
     * @return (array)具体浏览内容
     *
     * 如果是HR
     * @return order_wait_pay 待付款
     * @return order_wait_send 待发货
     * @return order_wait_take 待收货
     * @return order_wait_evaluate 待评价
     * @return collect_store 店铺收藏量
     * @return collect_goods 商品收藏
     * @return collect_act 活动收藏
     * @return collect_word 文档收藏
     * @return collect_news 新闻收藏
     *
     * 如果是供应商
     * @return member_fee 会费
     * @return bidding 投标中
     * @return winning_bid 已中标
     * @return act_sign_up 报名中的活动
     * @return act_finish 已举办的活动
     *
     * @return store_status 1审核成功 2审核失败
     * @return store_praise 好评量
     * @return store_collect 收藏量
     * @return store_call 致电量
     *
     * @return goods_num 上架的商品量
     * @return goods_salenum 商品销量
     * @return goods_collect 商品收藏量
     */
    public function userInfoApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $lists = Users::where(['id'=>$user_id])->where('status','<>',-1)->first();
            if (empty($lists)) return $this->writeJson(1,null,'用户不存在');
            if($lists['status'] == 1){
                $lists['is_mobile'] = 1;//是否绑定手机号：0否 1是
                $lists['is_wechat'] = empty($lists['wechat_id']) ? 0 : 1;//是否绑定微信：0否 1是
                $lists['is_qq'] = empty($lists['qq_id']) ? 0 : 1;//是否绑定QQ：0否 1是
                $lists['is_password'] = empty($lists['password']) ? 0 : 1;//是否设置密码：0否 1是
                $lists['footprint'] = Footprints::allLists(['user_id'=>$lists['id']], ['*'], 5);
                $lists['massage'] = Mails::where(['user_id'=>$lists['id'], 'status'=>1])->count();
                //判断当天是否签到
                $lists['is_sign'] = 0;//默认未签到
                if (trim(@date('Y-m-d', strtotime($lists['sign_in_at']))) == trim(date('Y-m-d'))) {
                    $lists['is_sign'] = 1;//已签到
                }
                if($lists['type'] == 1){
                    //收藏量
                    $collect_store = Collections::getCounts(['user_id'=>$lists['id'],'type'=>4]);
                    $lists['collect_store'] = strlen($collect_store)>=4 ? '999+' : $collect_store;
                    $collect_goods = Collections::getCounts(['user_id'=>$lists['id'],'type'=>3]);
                    $lists['collect_goods'] = strlen($collect_goods)>=4 ? '999+' : $collect_goods;
                    $collect_act = Collections::getCounts(['user_id'=>$lists['id'],'type'=>5]);
                    $lists['collect_act'] = strlen($collect_act)>=4 ? '999+' : $collect_act;
                    $collect_word = Collections::getCounts(['user_id'=>$lists['id'],'type'=>1]);
                    $lists['collect_word'] = strlen($collect_word)>=4 ? '999+' : $collect_word;
                    $collect_news = Collections::getCounts(['user_id'=>$lists['id'],'type'=>2]);
                    $lists['collect_news'] = strlen($collect_news)>=4 ? '999+' : $collect_news;
                    //订单量
                    $order_wait_pay = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>2]);
                    $lists['order_wait_pay'] = strlen($order_wait_pay)>=4 ? '999+' : $order_wait_pay;
                    $order_wait_send = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>3]);
                    $lists['order_wait_send'] = strlen($order_wait_send)>=4 ? '999+' : $order_wait_send;
                    $order_wait_take = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>4]);
                    $lists['order_wait_take'] = strlen($order_wait_take)>=4 ? '999+' : $order_wait_take;
                    $order_wait_evaluate = Orders::getCounts(['user_id'=>$lists['id'],'order_state'=>5]);
                    $lists['order_wait_evaluate'] = strlen($order_wait_evaluate)>=4 ? '999+' : $order_wait_evaluate;
                }else {
                    $lists['member_fee'] = isset($lists['member_fee']) ? '未缴纳' : '已缴纳';
                    //竞标
                    $bidding = BidLists::getCounts(['store_id'=>$lists['id'],'bid_status' => 3]);//投标中
                    $lists['bidding'] = strlen($bidding)>=4 ? '999+' : $bidding;
                    $winning_bid = BidLists::getCounts(['store_id'=>$lists['id'],'bid_status'=>2]);//已中标
                    $lists['winning_bid'] = strlen($winning_bid)>=4 ? '999+' : $winning_bid;
                    //活动
                    $now_time = date('Y-m-d H:i:s');
                    $act_sign_up = Activitys::getCounts(['user_id'=>$lists['id'],'enroll_at'=>$now_time]);
                    $lists['act_sign_up'] = strlen($act_sign_up)>=4 ? '999+' : $act_sign_up;
                    $act_finish = Activitys::getCounts(['user_id'=>$lists['id'],'start_at'=>$now_time]);
                    $lists['act_finish'] = strlen($act_finish)>=4 ? '999+' : $act_finish;
                    //店铺
                    $store_info = Stores::details(['user_id'=>$lists['id']], ['id','status','call','praise','collections']);
                    if (!empty($store_info) && $store_info['status'] == 1){
                        $lists['store_status'] = 1;
                    }else{
                        $lists['store_status'] = 2;
                    }
                    $lists['store_status'] = $store_info['status'];
                    $lists['store_call'] = strlen($store_info['call'])>=4 ? '999+' : $store_info['call'];
                    $lists['store_praise'] = strlen($store_info['praise'])>=4 ? '999+' : $store_info['praise'];
                    $lists['store_collect'] = strlen($store_info['collections'])>=4 ? '999+' : $store_info['collections'];
                    //商品
                    if($store_info['status'] == 1){
                        $goods_num = Goods::where(['store_id'=>$store_info['id'],'status'=>1])->count();
                        $lists['goods_num'] = strlen($goods_num)>=4 ? '999+' : $goods_num;
                        $collect = Goods::where(['store_id'=>$store_info['id'],'status'=>1])->get(['salenum','collect']);
                        foreach ($collect as $k=>$v){
                            $lists['goods_salenum'] += $v['salenum'];
                            $lists['goods_collect'] += $v['collect'];
                        }
                        $lists['goods_salenum'] = strlen($lists['goods_salenum'])>=4 ? '999+' : $lists['goods_salenum'];
                        $lists['goods_collect'] = strlen($lists['goods_collect'])>=4 ? '999+' : $lists['goods_collect'];
                    }
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户基本信息
     * @param url home.user.userBasicInfoApi
     * @param Request $request
     * @return bool
     */
    public function userBasicInfoApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where('id',$user_id)->where('status','<>',-1)->first(['id','user_sn','avatar','username','nickname','sex','mobile','email','type']);
            $infos = UserInfos::where('user_id',$user_id)->first();
            $userinfo['company_name'] = $infos['company_name'];
            $userinfo['company_email'] = $infos['company_email'];
            $userinfo['company_phone'] = $infos['company_phone'];
            $userinfo['master_name'] = $infos['master_name'];
            $userinfo['master_phone'] = $infos['master_phone'];
            $userinfo['master_position'] = $infos['master_position'];
            $userinfo['hr_department'] = $infos['hr_department'];
            $userinfo['hr_position'] = $infos['hr_position'];
            $userinfo['company_type_id'] = CompanyInfos::where('id',$infos['company_type'])->value('id');
            $userinfo['company_type_name'] = CompanyInfos::where('id',$infos['company_type'])->value('name');
            $userinfo['company_industry_id'] = CompanyInfos::where('id',$infos['company_industry'])->value('id');
            $userinfo['company_industry_name'] = CompanyInfos::where('id',$infos['company_industry'])->value('name');
            $userinfo['company_size_id'] = CompanyInfos::where('id',$infos['company_size'])->value('id');
            $userinfo['company_size_name'] = CompanyInfos::where('id',$infos['company_size'])->value('name');
            $userinfo['hr_size_id'] = CompanyInfos::where('id',$infos['hr_size'])->value('id');
            $userinfo['hr_size_name'] = CompanyInfos::where('id',$infos['hr_size'])->value('name');

            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            return $this->writeJson(0,$userinfo);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户基本信息修改
     * @param url home.user.userBasicInfoEditApi
     * @param avatar 头像
     * @param username 昵称
     * @param nickname 姓名
     * @param sex 性别 1男 2女
     * @param mobile 电话
     * @param email 邮箱
     * @param company_name 公司名称
     * @param company_type 公司类型
     * @param company_industry 公司行业
     * @param company_size 公司规模
     * @param hr_size HR规模
     * @param link_department HR部门
     * @param link_position HR职位
     * @param company_phone 供应商公司电话
     * @param company_email 供应商公司邮箱
     * @param master_name 供应商负责人姓名
     * @param master_phone 供应商负责人电话
     * @param master_position 供应商负责人职位
     * @return bool
     */
    public function userBasicInfoEditApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::getUserInfo(['id'=>$user_id]);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $userinfo->avatar = (string)$request->post('avatar');
            $userinfo->username = (string)$request->post('username');
            $userinfo->nickname = (string)$request->post('nickname');
            $userinfo->sex = (int)$request->post('sex');
            $userinfo->mobile = (string)$request->post('mobile');
            $userinfo->email = (string)$request->post('email');

            $userbascinfo = UserInfos::Where('user_id',$user_id)->first();
            $userbascinfo->company_name = (string)$request->post('company_name');
            $userbascinfo->company_phone = (string)$request->post('company_phone');
            $userbascinfo->company_email = (string)$request->post('company_email');
            $userbascinfo->company_type = (int)$request->post('company_type');
            $userbascinfo->company_size = (int)$request->post('company_size');
            $userbascinfo->company_industry = (int)$request->post('company_industry');
            $userbascinfo->hr_size = (int)$request->post('hr_size');
            $userbascinfo->hr_department = (string)$request->post('hr_department');
            $userbascinfo->hr_position = (string)$request->post('hr_position');
            $userbascinfo->master_name = (string)$request->post('master_name');
            $userbascinfo->master_phone = (string)$request->post('master_phone');
            $userbascinfo->master_position = (string)$request->post('master_position');

            $userinfo->save();
            $userbascinfo->save();
            return $this->writeJson(0,$userinfo);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }

    /**
     * 用户安全中心信息
     * @param url home.user.userSafeApi
     * @param Request $request
     * @return bool
     */
    public function userSafeApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::getUserInfo(['id'=>$user_id]);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $info['is_mobile'] = 1;//是否绑定手机号：0否 1是
            $info['is_wechat'] = empty($userinfo['wechat_id']) ? 0 : 1;//是否绑定微信：0否 1是
            $info['is_qq'] = empty($userinfo['qq_id']) ? 0 : 1;//是否绑定QQ：0否 1是
            $info['is_pass'] = empty($userinfo['password']) ? 0 : 1;//是否设置密码：0否 1是
            if(!empty($userinfo['mobile'])){
                $info['mobile'] = mb_substr($userinfo['mobile'],0,3,"utf-8").'*****'.mb_substr($userinfo['mobile'],-3,4,"utf-8");
            }else {
                $info['mobile'] = null;
            }
            return $this->writeJson(0,$info);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户修改密码
     * @param url home.user.userChangePwdApi
     * @param mobile 手机号
     * @param password 新密码
     * @param confirmc_pwd 确认密码
     * @return bool
     */
    public function userChangePwdApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $mobile = (string)$request->post('mobile');
            $password = (string)$request->post('password');
            $confirmc_pwd = (string)$request->post('confirmc_pwd');
            if(empty($mobile) || empty($password) || empty($confirmc_pwd))  return $this->writeJson(1,null,'参数错误');
            if(!preg_match('/^((13|14|15|16|17|18)[0-9]\d{8})$/', $mobile))   return $this->writeJson(1,null,'请填写正确的手机号码');
            if($password !== $confirmc_pwd){
                return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
            }
            $info = Users::where(['id'=>$user_id,'mobile'=>$mobile])->where('status','<>',-1)->first();
            if(!empty($info)){
                $info->mobile = $mobile;
                $info->password = bcrypt($password);
                $info->save();
                return $this->writeJson(0,route('home.login.logout'), '修改成功,请重新登录');
            }else{
                return $this->writeJson(1,null, '该用户不存在');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户修改手机号
     * @param url home.user.userChangeMobileApi
     * @param old_mobile 原手机号
     * @param new_mobile 新手机号
     * @return bool
     */
    public function userChangeMobileApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $old_mobile = (string)$request->post('old_mobile');
            $new_mobile = (string)$request->post('new_mobile');
            if(empty($old_mobile) || empty($new_mobile))  return $this->writeJson(1,null,'原手机号或新手机号不能为空');
            $str = '/^((13|14|15|16|17|18)[0-9]\d{8})$/';
            if(!preg_match($str, $old_mobile) || !preg_match($str, $new_mobile)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            if($old_mobile == $old_mobile){
                return $this->writeJson(1,null,'新手机号和原手机号相同，无需修改');
            }
            $info = Users::where(['id'=>$user_id,'mobile'=>$old_mobile])->where('status','<>',-1)->first();
            if(!empty($info)){
                $info->mobile = $new_mobile;
                $info->save();
                return $this->writeJson(0,route('home.login.logout'), '修改成功,请重新登录');
            }else{
                return $this->writeJson(1,null, '该用户不存在');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户资产管理信息
     * @param url home.user.userAssetApi
     * @return username 用户名
     * @return integral 积分
     * @return balance 余额
     * @return bond 保证金
     * @return profit 收益
     * @return contract 合同交易额
     * @return cash 提现
     * @return member_fee 会员费
     * @return member_fee_at 会员费有效期
     */
    public function userAssetApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $lists = Users::where(['id'=>$user_id])->where('status','<>',-1)
                ->first(['id','username','integral','balance','bond','profit','contract','cash','member_fee','member_fee_at']);
            if (empty($lists)) return $this->writeJson(1,null,'用户不存在');

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 积分记录列表
     * @param url home.user.hrAddressApi
     * @return user_name 用户名
     * @return integral 操作金额
     * @return desc 操作描述
     * @return created_at 添加时间
     */
    public function userIntegralLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $lists = IntegralLogs::allLists(['user_id'=>$user_id],['user_name','desc','integral','created_at'],$page,10);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 余额记录列表
     * @param url home.user.userBalanceLogsApi
     * @param Request $request
     * @return user_name 用户名
     * @return balance 操作金额
     * @return desc 操作描述
     * @return created_at 添加时间
     */
    public function userBalanceLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $lists = BalanceLogs::allLists(['user_id'=>$user_id],['user_name','desc','balance','created_at'],$page,10);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户提现
     * @param url home.user.userCashOutApi
     * @param amount 提现金额
     * @param bank_no 银行账户
     * @param bank_user 开户行姓名
     * @param phone 提现人手机号
     * @param bank_name 开户行
     * @return bool
     */
    public function userCashOutApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->first();
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $amount = $request->post("amount");
            $bank_no = (string)$request->post("bank_no");
            $bank_name = (string)$request->post("bank_name");
            $phone = (string)$request->post("phone");
            $bank_user = (string)$request->post("bank_user");
            if (empty($phone) || empty($amount) || empty($bank_name) || empty($bank_no) || empty($bank_user)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $phone)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            //验证金额是否足够
            if ($amount < 100) {
                return $this->writeJson(1,null,'提现金额不能低于100');
            }
            if($userinfo['type'] == 1){
                if (floatval($userinfo['balance']) < $amount) {
                    return $this->writeJson(1,null,'余额不足');
                }
            }else {
                if (floatval($userinfo['profit']) < $amount) {
                    return $this->writeJson(1,null,'总收益不足');
                }
            }

            $data = [];
            $data['pay_sn'] = Buys::makePaySn($user_id);
            $data['user_id'] = $user_id;
            $data['user_name'] = $userinfo['username'];
            $data['phone'] = $phone;
            $data['amount'] = $amount;
            $data['bank_name'] = $bank_name;
            $data['bank_no'] = $bank_no;
            $data['bank_user'] = $bank_user;
            $insert = CashOuts::create($data);
            if (!$insert) {
                return $this->writeJson(1,null,'提现失败');
            }
            return $this->writeJson(0,null,'提现成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户足迹
     * @param url home.user.userFootprintApi
     * @param page 页码
     * @return foot_id 浏览内容id
     * @return type 类型：1供应商 2需求 3商品 4活动 5文档
     * @return created_at 浏览时间
     * @return (array)具体浏览内容
     */
    public function userFootprintApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->value('id');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $footprint = Footprints::allLists(['user_id'=>$user_id], ['*'], 10, $page);

            return $this->writeJson(0,$footprint);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }

    /**
     * 用户站内消息列表
     * @param url home.user.userMailsListApi
     * @param page 页码
     * @param status 类型 1:未读 2:已读 不传或其他：全部
     * @return title 标题
     * @return content 内容
     * @return status 状态1未读2已读
     * @return created_at 创建时间
     */
    public function userMailsListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            $status = (int)$request->get('status');//类型 1:未读 2:已读
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $params = [];
            $params['user_id'] = $user_id;
            $params['status'] = $status;
            $list = Mails::allLists($params,['id','title','content','status','created_at'],$page,10);

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息详情
     * @param url home.user.userMailsDetailsApi
     * @param maile_id 消息id
     * @return id 消息id
     * @return user_id 用户id
     * @return title 标题
     * @return content 内容
     * @return status 状态1未读2已读
     * @return created_at 创建时间
     */
    public function userMailsDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $maile_id = (int)$request->get('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');

            $lists = Mails::details(['id'=>$maile_id,'user_id'=>$user_id], $fields=['*']);
            Mails::where('id',$maile_id)->update(['status'=>2]);//变为已读

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息标记为已读
     * @param url home.user.userMailsReadApi
     * @param maile_id 消息id(字符串：逗号隔开)
     * @return bool
     */
    public function userMailsReadApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $maile_id = $request->post('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$maile_id);

            Mails::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>2]);//变为已读

            return $this->writeJson(0,null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户站内消息删除
     * @param url home.user.userMailsDeleteApi
     * @param maile_id 消息id(字符串：逗号隔开)
     */
    public function userMailsDeleteApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $maile_id = $request->post('maile_id');//消息id
            if (empty($maile_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$maile_id);

            Mails::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>-1]);//软删除

            return $this->writeJson(0,null, '删除成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议列表
     * @param url home.user.userFeedbacksListApi
     * @param page 页码
     * @param isreply 是否回复 1:否 2:是 不传或其他：全部
     * @return id id
     * @return user_id 投诉人ID
     * @return content 投诉内容
     * @return created_at 添加时间
     * @return isreply 是否回复 1:否 2:是
     */
    public function userFeedbacksListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            $isreply = (int)$request->get('isreply');//是否回复 1:否 2:是
            if (empty($page)) return $this->writeJson(1,null,'参数错误');

            $lists = Feedbacks::allLists(['user_id'=>$user_id,'isreply'=>$isreply],['id','user_id','content','images','created_at','isreply'],$page,10);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议详情
     * @param url home.user.userFeedbacksDetailsApi
     * @param feed_id 建议id
     * @return content 用户发送的建议内容
     * @return images 用户发送的图片
     * @return created_at 用户发送的时间
     * @return isreply 是否回复 1:否 2:是
     * @return reply_content 平台回复的建议内容
     * @return reply_time 平台回复的时间
     */
    public function userFeedbacksDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $feed_id = (int)$request->get('feed_id');//建议id
            if (empty($feed_id)) return $this->writeJson(1,null,'参数错误');

            $lists = Feedbacks::details(['id'=>$feed_id,'user_id'=>$user_id], $fields=['*']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议添加
     * @param url home.user.userFeedbacksAddApi
     * @param content 内容
     * @param images 图片
     * @return bool
     */
    public function userFeedbacksAddApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $username = Users::where('id',$user_id)->value('username');
            if (empty($username)) return $this->writeJson(1,null,'用户不存在');
            $images = (string)$request->post('images');
            if (!empty($images)){
                $images = explode(',',$images);
            }

            $params = [
                'user_id' => $user_id,
                'user_name' => $username,
                'content' => (string)$request->post('content'),
                'images' => $images,
                'created_at' => date('Y-m-d H:i:s'),
            ];
            Feedbacks::create($params);
            return $this->writeJson(0,null, '添加成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 用户投诉建议删除
     * @param url home.user.userFeedbacksDelete
     * @param feed_id 建议id(字符串：逗号隔开)
     * @return bool
     */
    public function userFeedbacksDeleteApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $feed_id = $request->post('feed_id');//建议id
            if (empty($feed_id)) return $this->writeJson(1,null,'参数错误');
            $ids = explode(',',$feed_id);

            Feedbacks::where('user_id',$user_id)->whereIn('id',$ids)->update(['status'=>-1]);//删除
            return $this->writeJson(0,null, '删除成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址列表
     * @param url home.user.hrAddressApi
     * @param Request $request
     * @return bool
     */
    public function hrAddressApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $address = Addresses::allLists(['user_id'=>$user_id]);

            return $this->writeJson(0,$address);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址详情
     * @param url home.user.hrAddressDetailsApi
     * @param address_id 收货地址id
     * @return realname 姓名
     * @return phone 电话
     * @return province 省份
     * @return city 城市
     * @return region 区域
     * @return detail 详细地址
     */
    public function hrAddressDetailsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $address_id = (int)$request->get("address_id");//收货地址id
            if (empty($address_id)) return $this->writeJson(1,null,'参数错误');

            $address = Addresses::details(['id'=>$address_id,'user_id'=>$user_id]);

            return $this->writeJson(0,$address);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR用户收货地址新增或编辑
     * @param url home.user.hrAddressEditApi
     * @param realname 收货人
     * @param phone 电话
     * @param province 省份id
     * @param city 城市id
     * @param region 区域id
     * @param detail 详细地址
     * @return bool
     */
    public function hrAddressEditApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if ($userinfo != 1) return $this->writeJson(1,null,'供应商没有收获地址');
            $realname = (string)$request->post("realname");
            $phone = (string)$request->post("phone");
            $province = (int)$request->post("province");
            $city = (int)$request->post("city");
            $region = (int)$request->post("region");
            $detail = (string)$request->post("detail");
            if (empty($user_id) || empty($realname) || empty($phone) || empty($detail)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!empty($phone) && !preg_match('/^((13|14|15|16|17|18|19)[0-9]\d{8})$/', $phone)){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }
            $province_name = Regions::where('id',$province)->value('name');
            $city_name = Regions::where('id',$city)->value('name');
            $region_name = Regions::where('id',$region)->value('name');

            $address = Addresses::where(['user_id' => $user_id])->where('status','<>',-1)->first();
            if(!empty($address)){
                $address->user_id = $user_id;
                $address->realname = $realname;
                $address->phone = $phone;
                $address->province = $province;
                $address->city = $city;
                $address->region = $region;
                $address->area = $province_name . ' ' . $city_name . ' ' .$region_name;
                $address->detail = $detail;
                $address->default = 1;
            }else{
                $address = new Addresses();
                $address->user_id = $user_id;
                $address->realname = $realname;
                $address->phone = $phone;
                $address->province = $province;
                $address->city = $city;
                $address->region = $region;
                $address->area = $province_name . ' ' . $city_name . ' ' .$region_name;
                $address->detail = $detail;
                $address->default = 1;
            }
            $address->save();

            return $this->writeJson(0,null,'操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除收货地址
     * @param url home.user.hrAddressDeleteApi
     * @param address_id 收货地址id
     * @return bool
     */
    public function hrAddressDeleteApi(Request $request) {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $address_id = (int)$request->post("address_id");
            if (empty($address_id)) return $this->writeJson(1,null, '参数错误');

            Addresses::where(['id'=>$address_id, 'user_id'=>$user_id])->update(['status'=>-1]);
            return $this->writeJson(0,null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }

    /**
     * HR订单列表
     * @param url home.user.hrOrderListApi
     * @param page 页码
     * @param state_type 类型：1待付款 2待发货 3待收货 4待评价 不传或其他值：全部
     * @return id 订单id
     * @return order_sn 订单号
     * @return pay_sn 支付单号
     * @return store_id 店铺id
     * @return store_name 店铺名称
     * @return created_at 下单时间
     * @return order_amount 订单金额
     * @return pay_price 支付金额
     * @return order_state 订单状态：1:已取消 2:未付款 3:已付款 4:已发货 5:已收货
     * @return evaluation_state 评价状态 1：未评价 2：已评价
     * @return shipping_code 物流公司
     * @return shipping_sn 物流单号
     * @return pay_method 支付方式 1微信 2支付宝
     * @return is_cancel 是否显示取消订单：0否 1是
     * @return is_pay 是否显示付款：0否 1是
     * @return is_logistics 是否显示查看物流：0否 1是
     * @return is_take 是否显示收货：0否 1是
     * @return is_evaluation 是否显示评价：0否 1是
     * @return is_delete 是否显示删除：0否 1是
     * @return (array)goods_list 订单商品列表
     */
    public function hrOrderListApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get("page",1);
            $state_type = (int)$request->get("state_type");
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $params = [];
            $params['user_id'] = $user_id;
            if ($state_type == 1){
                $params['order_state'] = 2;
            }elseif ($state_type == 2){
                $params['order_state'] = 3;
            }elseif ($state_type == 3){
                $params['order_state'] = 4;
            }elseif ($state_type == 4){
                $params['order_state'] = 5;
                $params['evaluation_state'] = 1;
                $params['refund_state'] = 0;
            }
            $fields = ['id','order_sn','pay_sn','store_id','store_name','created_at','order_amount','pay_price','order_state','evaluation_state','shipping_code','shipping_sn','pay_method'];
            $order_list = Orders::allLists($params,$fields,$page,10,'id','desc',['order_goods']);

            foreach ($order_list as $key => $order) {
                //默认该订单的不可操作性
                $order['is_cancel'] = 0;//取消订单
                $order['is_pay'] = 0;//付款
                $order['is_logistics'] = 0;//查看物流
                $order['is_take'] = 0;//收货
                $order['is_evaluation'] = 0;//评价
                $order['is_delete'] = 0;//删除
                //显示取消订单
                if($order['order_state'] == 2){
                    $order['is_cancel'] = 1;
                    $order['is_pay'] = 1;
                }
                if($order['order_state'] == 3){
                    $order['is_cancel'] = 1;
                }
                //显示收货
                if($order['order_state'] == 4){
                    $order['is_logistics'] = 1;
                    $order['is_take'] = 1;
                }
                //显示评价
                if($order['order_state'] == 5 && $order['evaluation_state'] == 1){
                    $order['is_evaluation'] = 1;
                }
                //显示删除订单
                if($order['order_state'] == 1 || $order['order_state'] == 5){
                    $order['is_delete'] = 1;
                }
                //取商品列表
                $order_list[$key]['goods_list'] =OrderGoods::where('order_id',$order['id'])->get()->toArray();
            }

            return $this->writeJson(0,$order_list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR订单详情
     * @param url home.user.hrOrderDetailsApi
     * @param order_id 订单号
     * @return order_sn 订单id
     * @return pay_sn 支付单号
     * @return order_amount 订单金额
     * @return is_cancel 是否显示取消订单：0否 1是
     * @return is_pay 是否显示付款：0否 1是
     * @return is_logistics 是否显示查看物流：0否 1是
     * @return is_take 是否显示收货：0否 1是
     * @return is_evaluation 是否显示评价：0否 1是
     * @return is_delete 是否显示删除：0否 1是
     */
    public function hrOrderDetailsApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $order_id = (int)$request->get("order_id",0);
            if(!$order_id || $order_id <= 0) return $this->writeJson(1,null,'该订单不存在');

            $params = [];
            $params['order_id'] = $order_id;
            $params['user_id'] = $user_id;
            $order_info = Orders::getOrderInfo($params,['*'],['order_goods']);
            //默认该订单的不可操作性
            $order_info['is_cancel'] = 0;//取消订单
            $order_info['is_pay'] = 0;//付款
            $order_info['is_logistics'] = 0;//查看物流
            $order_info['is_take'] = 0;//收货
            $order_info['is_evaluation'] = 0;//评价
            $order_info['is_delete'] = 0;//删除
            //显示取消订单
            if($order_info['order_state'] == 2){
                $order_info['is_cancel'] = 1;
                $order_info['is_pay'] = 1;
            }
            if($order_info['order_state'] == 3){
                $order_info['is_cancel'] = 1;
            }
            //显示收货
            if($order_info['order_state'] == 4){
                $order_info['is_logistics'] = 1;
                $order_info['is_take'] = 1;
            }
            //显示评价
            if($order_info['order_state'] == 5 && $order_info['evaluation_state'] == 1){
                $order_info['is_evaluation'] = 1;
            }
            //显示删除订单
            if($order_info['order_state'] == 1 || $order_info['order_state'] == 5){
                $order_info['is_delete'] = 1;
            }
            $order_info['created_at'] = $order_info['created_at'];
            $order_info['pay_time'] = $order_info['pay_time'];
            $order_info['shipping_time'] = $order_info['shipping_time'];
//            $order_info['reciver_info'] = unserialize($order_info['reciver_info']);
            return $this->writeJson(0,$order_info);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR订单状态操作
     * @param url home.user.hrOrderChangeStateApi
     * @param order_id 订单号
     * @param state_type 类型：1:取消订单  2:确认收货  3:删除订单
     */
    public function hrOrderChangeStateApi(Request $request) {
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $order_id = (int)$request->post("order_id");
            $state_type = (int)$request->post("state_type");
            if (empty($order_id) || empty($state_type)) return $this->writeJson(1,null,'参数错误');

            $params = [];
            $params['id'] = $order_id;
            $params['user_id'] = $user_id;
            $order_info = Orders::getOrderInfo($params,['*']);
            if (empty($order_info)) return $this->writeJson(1,null,'该订单不存在');

            if ($state_type == 1) {//取消订单
                if($order_info['order_state'] == 2 || $order_info['order_state'] == 3){
                    Orders::changeOrderStateCancel($order_info);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else if ($state_type == 2) {//确认收货
                if($order_info['order_state'] == 4){
                    //更新订单状态
                    $update_order = ['order_state' => 5, 'finnshed_time'=>date('Y-m-d H:i:s')];
                    Orders::where('id',$order_id)->update($update_order);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else if ($state_type == 3) {
                if($order_info['order_state'] == 1 || $order_info['order_state'] == 5){
                    //删除订单
                    OrderGoods::where('order_id',$order_id)->update(['status'=>-1]);
                    Orders::where('id',$order_id)->update(['status'=>-1]);
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            } else {
                return $this->writeJson(1,null,'参数错误');
            }
            return $this->writeJson(0,null,'操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR评价商品
     * @param url home.user.hrGoodsEvaluatesApi
     * @param goods_id 商品id
     * @param goods_name 商品名称
     * @param content 内容
     * @param images 图片（最多5张）
     * @param type 1:好评 2:中评 3:差评
     * @param Request $request
     * @return bool
     */
    public function hrGoodsEvaluatesApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if($userinfo != 1)  return $this->writeJson(1,null,'HR才可以评价商品');

            $evaluate_array = (array)$request->post('evaluate_array');//评价数组
            if (empty($evaluate_array)) return $this->writeJson(1,null,'参数不正确');

            //获取订单信息
            $order_info = Orders::getOrderInfo(['order_id'=>$evaluate_array['order_id']],['user_id','order_state']);
            //判断订单身份
            if ($order_info['user_id'] != $user_id) return $this->writeJson(1,null,'参数不正确');
            //订单为'已收货'状态，并且未评论
            if($order_info['order_state'] != 5 && $order_info['evaluation_state'] != 1){
                return $this->writeJson(1,null,'订单信息错误');
            }
            //获取订单商品
            $order_goods =OrderGoods::where('order_id',$evaluate_array['order_id'])->get();
            if (empty($order_goods)) return $this->writeJson(1,null,'订单信息错误');

            $insert = [];
            foreach ($order_goods as $value){
                //重组数据，获取星级、内容、图片数组
                $arr = [];
                foreach ($evaluate_array['comment'] as $k=>$v){
                    $arr[$v['goods_id']]['comment'] = $v['comment'];
                    $arr[$v['goods_id']]['images'] = $v['images'];
                    $arr[$v['goods_id']]['type'] = $v['type'];
                }
                //获取当前商品的评价图片
                $evaluate_image = serialize($arr[$value['goods_id']]['images']);//序列化
                //评分
                $evaluate_type = intval($arr[$value['goods_id']]['type']);
                //默认评语
                $evaluate_comment = $arr[$value['goods_id']]['comment'];
                if (empty($evaluate_comment)) {
                    $evaluate_comment = '不错哦';
                }
                $params = [
                    'user_id' => $user_id,
                    'user_name' => $userinfo['username'],
                    'user_avatar' => $userinfo['avatar'],
                    'goods_id' => $value['goods_id'],
                    'goods_name' => $value['goods_name'],
                    'content' => $evaluate_comment,
                    'images' => $evaluate_image,
                    'type' => $evaluate_type,
                ];
                if($params['type'] == 1){
                    $params['average_star'] = 5;//好评
                }elseif ($params['type'] == 2){
                    $params['average_star'] = 3;//中评
                }elseif ($params['type'] == 3){
                    $params['average_star'] = 1;//差评
                }
                array_push($insert, $params);
            }
            GoodsEvaluates::insert($insert);
            Goods::where('id',$params['goods_id'])->increment('praise');//评论量加1
            //计算好评率
            $all = GoodsEvaluates::where(['goods_id'=>$params['goods_id']])->where('status','<>',-1)->count();
            $goods = GoodsEvaluates::where(['goods_id'=>$params['goods_id'], 'type'=>1])->where('status','<>',-1)->count();
            $credit = $goods/$all*100;
            //被评价商品星级
            if($credit <= 20){
                $star = 1;
            }elseif ($credit >20 && $credit <= 40){
                $star = 2;
            }elseif ($credit >40 && $credit <= 60){
                $star = 3;
            }elseif ($credit >60 && $credit <= 80){
                $star = 4;
            }elseif ($credit >80 && $credit <= 100){
                $star = 5;
            }
            Goods::where('id',$params['goods_id'])->update(['credit'=>$credit, 'star'=>$star]);
            return $this->writeJson(0, null, '评价成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR需求管理中心列表
     * @param url home.user.hrDemandListApi
     * @param page 页码
     * @param state 状态 1:审核中 2:已通过 3:已完成 4:进行中 不传或其他：全部
     * @return id 需求id
     * @return user_id 发布需求人id
     * @return title 需求名称
     * @return price 需求预算
     * @return number 竞标人数
     * @return bid_state 状态
     * @return (array)bid_list 竞标供应商
     * @return id 投标id
     * @return store_id 供应商id
     * @return store_name 供应商名称
     * @return bid_status 中标状态 1未中标 2已中标 3投标中
     */
    public function hrDemandListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'页码不能为空');
            $state = (int)$request->get('state');//状态
            if($state == 1){//审核中
                $params['status'] = 2;
            }elseif ($state == 2){
                $params['status'] = 1;
            }elseif ($state == 3){
                $params['status'] = 1;
                $params['bid'] = 3;
            }elseif ($state == 4){
                $params['status'] = 1;
                $params['bid'] = 2;
            }
            $params['user_id'] = $user_id;
            $fields = ['id','user_id','title','price','status','bid','number'];
            $lists = Demands::allLists($params, $fields, $page, 10);
            foreach ($lists as $key => $val){
                if($val['status'] == 2){
                    $lists[$key]['state'] = 1;
                }elseif ($val['status'] == 1){
                    $lists[$key]['state'] = 2;
                    if($val['bid'] == 3){
                        $lists[$key]['state'] = 3;
                    }elseif ($val['bid'] == 2){
                        $lists[$key]['state'] = 4;
                    }
                }
                $lists[$key]['bid_list'] = BidLists::allLists(['user_id'=>$user_id,'demand_id'=>$val['id']], ['id','store_id','store_name','bid_status']);
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR收藏列表
     * @param url home.user.hrCollectListApi
     * @param page 页码
     * @param type 类型 1文档，2新闻，3商品，4店铺，5活动 (必传)
     * @return id 收藏id
     * @return collect_id 收藏内容id
     * @return type 收藏类型 1文档，2新闻，3商品，4店铺，5活动
     */
    public function hrCollectListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $type = (int)$request->get('type');//类型 1文档，2新闻，3商品，4店铺，5活动
            $page = (int)$request->get('page',1);//页码
            if (empty($type) || empty($page)) return $this->writeJson(1,null,'参数错误');

            $lists = Collections::allLists(['user_id'=>$user_id, 'type'=>$type], ['id','collect_id','type'],$page,10);

            foreach ($lists as $key=>$val) {
                if ($val['type'] == 1){
                    $fields = ['id','thumb','title','summary','view','download','format','cate_id','cate_pid'];
                    $word = Words::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['word_id'] = $word['id'];
                    $lists[$key]['word_thumb'] = $word['thumb'];
                    $lists[$key]['word_title'] = $word['title'];
                    $lists[$key]['word_summary'] = $word['summary'];
                    $lists[$key]['word_view'] = strlen($word['view'])>=4 ? '999+' : $word['view'];
                    $lists[$key]['word_download'] = strlen($word['download'])>=4 ? '999+' : $word['download'];
                    $lists[$key]['word_format'] = $word['format'];
                }elseif ($val['type'] == 2){
                    $fields = ['id','thumb','title','content','view','collect','created_at'];
                    $news = Articles::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['news_id'] = $news['id'];
                    $lists[$key]['news_thumb'] = $news['thumb'];
                    $lists[$key]['news_title'] = $news['title'];
                    $lists[$key]['news_content'] = str_limit(strip_tags($news['content']) , 170 , '...' );//str_limit 字符截取 strip_tags 去除HTML标签
                    $lists[$key]['news_view'] = strlen($news['view'])>=4 ? '999+' : $news['view'];
                    $lists[$key]['news_collect'] = strlen($news['collect'])>=4 ? '999+' : $news['collect'];
                    $lists[$key]['news_created_at'] = $news['created_at'];
                }elseif ($val['type'] == 3){
                    $fields = ['id','title','thumb','price','description','view','salenum','type','cate_id','cate_pid'];
                    $goods = Goods::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['goods_id'] = $goods['id'];
                    $lists[$key]['goods_thumb'] = $goods['thumb'];
                    $lists[$key]['goods_title'] = $goods['title'];
                    $lists[$key]['goods_price'] = $goods['price'];
                    $lists[$key]['goods_description'] = $goods['description'];
                    $lists[$key]['goods_type'] = $goods['type'];
                    $lists[$key]['goods_view'] = strlen($goods['view'])>=4 ? '999+' : $goods['view'];
                    $lists[$key]['goods_salenum'] = strlen($goods['salenum'])>=4 ? '999+' : $goods['salenum'];
                }elseif ($val['type'] == 4){
                    $fields = ['id','store_name','logo','call','view','service','praise','service_scope','hot','quality'];
                    $store = Stores::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['store_id'] = $store['id'];
                    $lists[$key]['store_name'] = $store['store_name'];
                    $lists[$key]['store_logo'] = $store['logo'];
                    $lists[$key]['store_view'] = strlen($store['view'])>=4 ? '999+' : $store['view'];
                    $lists[$key]['store_call'] = strlen($store['call'])>=4 ? '999+' : $store['call'];
                    $lists[$key]['store_service'] = strlen($store['service'])>=4 ? '999+' : $store['service'];
                    $lists[$key]['store_praise'] = strlen($store['praise'])>=4 ? '999+' : $store['praise'];
                    $lists[$key]['store_service_scope'] = $store['store_service_scope'];
                    $lists[$key]['store_hot'] = $store['hot'];
                    $lists[$key]['store_quality'] = $store['quality'];
                }elseif ($val['type'] == 5){
                    $now_time = date('Y-m-d H:i:s');
                    $fields = ['id','thumb','title','cate_pid','cate_id','view','cost','number','enroll_at','start_at'];
                    $act = Activitys::details(['id'=>$val['collect_id']], $fields);
                    $lists[$key]['act_id'] = $act['id'];
                    $lists[$key]['act_thumb'] = $act['thumb'];
                    $lists[$key]['act_title'] = $act['title'];
                    $lists[$key]['act_cost'] = $act['cost'];
                    $lists[$key]['act_view'] = strlen($act['view'])>=4 ? '999+' : $act['view'];
                    $lists[$key]['act_number'] = strlen($act['number'])>=4 ? '999+' : $act['number'];
                    $lists[$key]['act_enroll_at'] = $act['enroll_at'];
                    $lists[$key]['act_start_at'] = $act['start_at'];
                    if($now_time <= $act['enroll_at']){
                        $lists[$key]['act_state'] = '报名中';
                    }elseif ($now_time >= $act['start_at']){
                        $lists[$key]['act_state'] = '已举办';
                    }else {
                        $lists[$key]['act_state'] = '报名结束';
                    }
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR取消收藏
     * @param url home.user.hrCollectCancelApi
     * @param collect_id 收藏id
     * @return type 类型 1文档，2新闻，3商品，4店铺，5活动 (不能不传)
     * @return bool
     */
    public function hrCollectCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $collect_id = (int)$request->post('collect_id');//收藏内容id
            $type = (int)$request->post('type',1);//类型 1文档，2新闻，3商品，4店铺，5活动
            if (empty($type) || empty($collect_id)) return $this->writeJson(1,null,'参数错误');
            Collections::where(['user_id'=>$user_id, 'id'=>$collect_id,'type'=>$type])->delete();

            return $this->writeJson(0,null, '取消收藏成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR文档下载记录列表
     * @param url home.user.hrWordListApi
     * @return id 下载记录id
     * @return word_id 文档id
     * @return user_id 下载人id
     * @return created_at 下载时间
     * @return word_id 文档id
     * @return word_title 文档标题
     * @return word_format 文档格式
     */
    public function hrWordListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $list = WordDownloadLogs::allLists(['user_id'=>$user_id],['*'],$page,10);
            foreach ($list as $key => $val){
                $word = Words::where('id',$val['word_id'])->first(['id','title','format']);
                $list[$key]['word_id'] = $word['id'];
                $list[$key]['word_title'] = $word['title'];
                $list[$key]['word_format'] = $word['format'];
            }

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR删除文档下载记录
     * @param url home.user.hrWordCancelApi
     * @param download_id 下载记录id
     */
    public function hrWordCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $download_id = (int)$request->post('download_id');//下载记录id
            if (empty($download_id)) return $this->writeJson(1,null,'参数错误');
            WordDownloadLogs::where(['user_id'=>$user_id, 'id'=>$download_id])->update(['status'=>-1]);

            return $this->writeJson(0,null, '删除文档下载记录成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * HR活动管理中心列表
     * @param url home.user.hrActListApi
     * @param page 页码
     * @param type 类型 1:进行中 2:已举办 3:已取消 不传或其他:全部
     * @return id 活动id
     * @return title 活动标题
     * @return sponsor 活动主办方
     * @return start_at 活动开始时间
     * @return act_state 活动状态 1:进行中 2:已举办 3:已取消
     */
    public function hrActListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            $type = (int)$request->get('type');//类型 1:进行中 2:已举办 3:已取消 不传或其他:全部
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $now_time = date('Y-m-d H:i:s');
            $params = [];
//            $params['status'] = 1;
            if($type == 1){
                $params['enroll_at'] = $now_time;
            }elseif ($type == 2){
                $params['start_at'] = $now_time;
            }elseif ($type == 3){
                $params['status'] = 4;
            }

            $activity_id = [];
            $info = SignUps::where('user_id',$user_id)->where('status','<>',-1)->get(['id','activity_id','user_id'])->toArray();
            if (empty($info)){
                $list = new LengthAwarePaginator(
                    array_slice([], ($page * 10) - 10, 10, false),
                    count([]),
                    10, $page,
                    ['path' => $request->url(), 'query' => $request->query()]
                );
            }else {
                foreach ($info as $key => $val){
                    array_push($activity_id, $val['activity_id']);
                }
                $params['id'] = $activity_id;
                $list = Activitys::allLists($params,['id','title','sponsor','start_at','cate_pid','cate_id','status'],$page,10,'start_at');
                foreach ($list as $k=>$v){
                    if($v['status'] == 1){
                        if($now_time <= $v['start_at']){
                            $list[$k]['act_state'] = 1;//进行中
                        }else {
                            $list[$k]['act_state'] = 2;//已举办
                        }
                    }elseif ($v['status'] == 4) {
                        $list[$k]['act_state'] = 3;//已取消
                    }
                }
            }

            return $this->writeJson(0,$list);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }


    /**
     * 供应商审核结果
     * @param url home.user.hrAddressApi
     * @param Request $request
     * @return status 1:审核中 0:审核失败
     */
    public function supplierExamineApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->value('status');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if($userinfo == 2){
                $lists['status'] = 1;
            }elseif ($userinfo == 3){
                $lists['status'] = 0;
            }elseif ($userinfo == 1){
                return redirect(route('home.suppliermy.index'));
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商审核失败再次上传认证资料
     * @param url home.user.supplierUploadDataApi
     * @param certification 认证资料（数组）
     */
    public function supplierUploadDataApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->value('status');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $certification = $request->post('certification');
            if (empty($certification)) return $this->writeJson(1,null,'参数错误');
            $certifications = explode(',',$certification);
            UserInfos::where('user_id',$user_id)->update(['certification'=>$certifications]);
            Users::where('id',$user_id)->update(['status'=>2]);
            return $this->writeJson(0,null,'上传成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商收益记录列表
     * @param url home.user.supplierProfitLogsApi
     * @return type 类型：1商品、活动收益 2合同收益 不传或其他:所有收益
     * @return profit 金额
     * @return desc 描述
     */
    public function supplierProfitLogsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $lists = ProfitLogs::allLists(['user_id'=>$user_id],['*'],$page,10);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商申请退货保证金
     * @param url home.user.supplierBondReturnApi
     * @param bond 退还金额
     * @param desc 原因
     * @param link_name 联系人
     * @param link_phone 联系电话
     */
    public function supplierBondReturnApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->first(['username','bond']);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $data = [];
            $data['user_id'] = $user_id;
            $data['user_name'] = $userinfo['username'];
            $data['bond'] = (int)$request->post('bond');
            $data['desc'] = (string)$request->post('desc');
            $data['link_name'] = (string)$request->post('link_name');
            $data['link_phone'] = (string)$request->post('link_phone');
            if (empty($data['bond']) ||empty($data['desc']) ||empty($data['link_name']) ||empty($data['link_phone'])) return $this->writeJson(1,null,'参数错误');

            if($data['bond'] !== (int)$userinfo['bond']){
                return $this->writeJson(1,null,'退还金额错误');
            }
            BondReturnLogs::create($data);
            return $this->writeJson(0,null,'申请成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商竞标管理中心列表
     * @param url home.user.supplierDemandListApi
     * @param page 页码
     * @param bid_status 状态 1:未中标 2:已中标 3:已投标
     * @return demand_id 需求id
     * @return demand_title 需求标题
     * @return demand_price 需求预算
     * @return bid_status 状态 1:未中标 2:已中标 3:已投标
     */
    public function supplierDemandListApi(Request $request){
        try {
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'页码不能为空');
            $params = [
                'store_id' => $user_id,
                'bid_status' => (int)$request->get('bid_status'),
            ];
            $lists = BidLists::allLists($params, ['id','demand_id','bid_status'],10 ,$page);
            foreach ($lists as $key => $val){
                $demand = Demands::where('id',$val['demand_id'])->where('status','<>',-1)->first(['id','title','price']);
                $lists[$key]['demand_title'] = $demand['title'];
                $lists[$key]['demand_price'] = $demand['price'];
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商活动管理中心列表
     * @param url home.user.supplierActListApi
     * @param page 页码
     * @param type 类型 1:报名中 2:已举办 不传或其他:全部
     * @return act_state 1:报名中 2:进行中 3:已举办
     */
    public function supplierActListApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            $type = (int)$request->get('type');//类型 1:报名中 2:进行中 3:已举办 不传或其他:全部
            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $now_time = date('Y-m-d H:i:s');
            $params = [];
            $params['user_id'] = $user_id;
            $params['status'] = 1;
            if($type == 1){
                $params['enroll_at'] = $now_time;
            }elseif ($type == 2){
                $params['now_at'] = $now_time;
            }elseif ($type == 3){
                $params['start_at'] = $now_time;
            }
            $lists = Activitys::allLists($params,['id','title','number','enroll_at','start_at','cate_pid','cate_id','status'],$page,10,'start_at');
            foreach ($lists as $K=>$v){
                if($now_time <= $v['enroll_at']){
                    $lists[$K]['act_state'] = 1;
                }elseif ($now_time > $v['enroll_at'] && $now_time < $v['start_at']) {
                    $lists[$K]['act_state'] = 2;
                }else {
                    $lists[$K]['act_state'] = 3;
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商取消活动
     * @param url home.user.supplierActCancelApi
     * @param act_id 活动id
     * @return status 1:审核中 0:审核失败
     */
    public function supplierActCancelApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->value('status');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            
            $act_id = (int)$request->post('act_id');
            if (empty($act_id)) return $this->writeJson(1,null,'参数错误');
            Activitys::where(['id'=>$act_id,'user_id'=>$user_id])->update(['status'=>4]);

            return $this->writeJson(0,null, '取消成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商店铺基本信息
     * @param url home.user.supplierStoreApi
     * @return logo logo
     * @return store_name 名称
     * @return service_area 服务区域（调：home.public.area）
     * @return area_info 地址
     * @return workingtime 工作时间
     * @return description 简介
     * @return image 店铺图片
     * @return partners 合作商
     * @return cid 擅长领域
     * @return services 服务内容
     * @return cases 成功案例
     */
    public function supplierStoreApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->value('status');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $fields = ['id','logo','store_name','service_area','area_info','cate_pid','cate_id','workingtime','description','image','partners'];
            $lists = Stores::where(['user_id'=>$user_id])->where('status','<>',-1)->first($fields);
            if(!empty($lists)){
                //将一级分类和二级分类拼接
                if(!empty($lists['cate_id']) && !empty($lists['cate_pid'])){
                    $ids = $lists['cate_id'].",".$lists['cate_pid'];
                }else{
                    if(!empty($lists['cate_id'])){
                        $ids = $lists['cate_id'];
                    }elseif (!empty($lists['cate_pid'])){
                        $ids = $lists['cate_pid'];
                    }
                }
                $ids = !empty($ids) ? explode(',',$ids) : 0;

                foreach ($ids as $k => $v){
                    $pid=Categorys::getPid($v);
                    if($pid > 0){
                        $pids[$k][]="$pid";
                        $pids[$k][]=$v;
                    }else{
                        $pids[$k][]=$v;
                    }
                }
                $lists['cid'] = $pids;
                //擅长领域
                $lists['cate_id'] = explode(',',$lists['cate_id']);
                foreach ($lists['cate_id'] as $k => $v){
                    $pidTree[] =Categorys::where('id',$v)->first('name');
                }
                $lists['cate_id'] = $pidTree;
                $lists['workingtime'] = explode(' - ',$lists['workingtime']);
                $lists['service_area_name'] = Areas::where(['id' => $lists['service_area']])->where('status','<>',-1)->value('name');
                $lists['services'] = Services::where(['store_id' => $lists['id']])->where('status','<>',-1)->get(['title','thumb','images','content']);
                $lists['cases'] = Cases::where(['store_id' => $lists['id']])->where('status','<>',-1)->get(['title','thumb','images','content']);
//                foreach ($lists['cases'] as $key => $val){
//                    if(!empty($val['images'])){
//                        foreach ($val['images'] as $k=>$v){
//                            dd($v);
//                            $cases_img[$key]['name'] = substr($v,strripos($v,"/")+1);
//                            $cases_img[$key]['url'] = $v;
//                        }
//                        $lists['cases'][$key]['images'] = json_encode($cases_img);
//                    }
//                }
//
//                foreach ($lists['services'] as $key => $val){
//                    if(!empty($val['images'])){
//                        foreach ($val['images'] as $k=>$v){
//                            $services_img[$key]['name'] = substr($v,strripos($v,"/")+1);
//                            $services_img[$key]['url'] = $v;
//                        }
//                        $lists['services'][$key]['images'] = json_encode($services_img);
//                    }
//                }

                if(!empty($lists['image'])){
                    foreach ($lists['image'] as $key => $val){
                        $images[$key]['name'] = substr($val,strripos($val,"/")+1);
                        $images[$key]['url'] = $val;
                    }
                    $lists['image'] = $images;
                }
                if(!empty($lists['partners'])){
                    foreach ($lists['partners'] as $key => $val){
                        $partners[$key]['name'] = substr($val,strripos($val,"/")+1);
                        $partners[$key]['url'] = $val;
                    }
                    $lists['partners'] = $partners;
                }
            }else{
                $lists = [];
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商开店、修改店铺信息
     * @param url home.user.supplierStoreUpdateApi
     * @param cid 分类(数组)
     * @param logo logo
     * @param store_name 店铺名
     * @param area_info 地址
     * @param service_area 区域（id）
     * @param workingtime 工作时间
     * @param description 简介
     * @param image 图片(逗号隔开字符串)
     * @param partners 合作伙伴(逗号隔开字符串)
     * @param case 成功案例(二维数组)
     * @param service 服务内容(二维数组)
     */
    public function supplierStoreUpdateApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->first(['username']);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $workingtime_str = (string)$request->post("workingtime");//工作时间
            $workingtime =  str_replace(',', '-', $workingtime_str);//字符串替换
            $image_str = $request->post("image");//图片
            $image = explode(',',$image_str);
            $partners_str = $request->post("partners");//合作商
            $partners = explode(',',$partners_str);

            //处理分类
            $cid = json_decode($request->post("cid"));//分类
            $str1 = [];
            $str2 = [];
            foreach ($cid as $k=>$v) {
                array_push($str1, $v[0]);
                if (isset($v[1])){
                    array_push($str2, $v[1]);
                }
            }
            $cate_pid = implode(",",array_unique($str1));
            $cate_id = implode(",",array_unique($str2));

            //json转数组
            $case = json_decode($request->post("case"));//成功案例
            $service = json_decode($request->post("service"));//服务内容
            foreach ($case as $k => $v){
                $case_arr[] = (array)$this->objectToArray($v);
            }
            foreach ($service as $k => $v){
                $service_arr[] = (array)$this->objectToArray($v);
            }

            $store = Stores::where('user_id',$user_id)->where('status','<>',-1)->first();
            if (empty($store)){
                $store = new Stores();
            }
            $store->user_id = $user_id;
            $store->seller_name = $userinfo['username'];
            $store->logo = (string)$request->post("logo");
            $store->store_name = (string)$request->post("store_name");
            $store->area_info = (string)$request->post("area_info");
            $store->service_area = (int)$request->post("service_area");
            $store->workingtime = $workingtime;
            $store->description = (string)$request->post("description");
            $store->image = $image;
            $store->partners = $partners;
            $store->cate_pid = $cate_pid;
            $store->cate_id = $cate_id;
            $store->save();

            $store_id = Stores::where('user_id',$user_id)->where('status','<>',-1)->value('id');
            //先删除再添加
            $result = Cases::where('store_id',$store_id)->get(['id']);
            foreach ($case_arr as $value){
                $data = [
                    'store_id'  => $store_id,
                    'thumb'     => $value['imageUrl'],
                    'title'     => $value['title'],
                    'images'   => $value['contentImg'],
                    'content'   => $value['content'],
                ];
                if(empty($result)){
                    Cases::create($data);
                }else{
                    foreach($result as $v){
                        Cases::destroy($v['id']);
                    }
                    Cases::create($data);
                }
            }

            //先删除再添加
            $result = Services::where('store_id',$store_id)->where('status','<>',-1)->get(['id']);
            foreach ($service_arr as $value){
                $data = [
                    'store_id'  => $store_id,
                    'thumb'     => $value['imageUrl'],
                    'title'     => $value['title'],
                    'images'   => $value['contentImg'],
                    'content'   => $value['content'],
                ];
                if(empty($result)){
                    Services::create($data);
                }else{
                    foreach($result as $v){
                        Services::destroy($v['id']);
                    }
                    Services::create($data);
                }
            }
            return $this->writeJson(0,null,'操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品管理中心列表
     * @param url home.user.supplierGoodsListsApi
     * @param Request $request
     * @return bool
     * @return type 商品类型：1非实物 2实物 3积分兑换
     * @return status 状态：1正常 2通过 3未通过 4下架
     */
    public function supplierGoodsListsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->first(['username']);
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $page = (int)$request->get('page',1);//页码
            if (empty($page)) return $this->writeJson(1,null,'参数错误');

            $store_id = Stores::where('user_id',$user_id)->where('status','<>',-1)->value('id');
            if (empty($store_id)) return $this->writeJson(1,null,'您还未开店，请先去开店');

            $fields = ['id','title','thumb','price','description','view','salenum','type','status','cate_id','cate_pid'];
            $lists = Goods::apiLists(['store_id'=>$store_id], $fields, $page, 10);
            foreach ($lists as $key=>$val){
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
                $lists[$key]['salenum'] = strlen($val['salenum'])>=4 ? '999+' : $val['salenum'];
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品基本信息
     * @param url home.user.supplierGoodsApi
     * @param Request $request
     */
    public function supplierGoodsApi(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id])->value('status');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $good_id = (int)$request->get('good_id',15);
            if (empty($good_id)) return $this->writeJson(1,null,'参数错误');
            $lists = Goods::details(['id'=>$good_id]);
            $lists['cid'] = [$lists['cate_pid'],$lists['cate_id']];
            $lists['categorys'] = GoodsCategorys::getCascaderList();//获取分类
            foreach ($lists['images'] as $key => $val){
                $images[$key]['name'] = substr($val,strripos($val,"/")+1);
                $images[$key]['url'] = $val;
            }
            $lists['images'] = $images;

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商添加、修改商品
     * @param url home.user.supplierGoodsUpdateApi
     * @param Request $request
     * @param thumb 封面图
     * @param title 标题
     * @param cate_pid 一级分类id
     * @param cate_id 二级分类id
     * @param description 简介
     * @param content 描述
     * @param images 多图（逗号隔开）
     * @param price 价格
     * @param link_name 联系人
     * @param link_phone 联系人电话
     * @param link_department 联系人部门
     * @param link_position 联系人职位
     * @param storage 库存
     * @param freight 运费
     * @param type 类型：1非实物 2实物
     */
    public function supplierGoodsUpdateApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if($userinfo != 2)  return $this->writeJson(1,null,'HR不能发布产品');
            $store = Stores::where(['user_id'=>$user_id, 'status'=>1])->where('status','<>',-1)->first(['id','store_name']);
            if (empty($store)) return $this->writeJson(1,null,'请先开店');

            $good_id = (int)$request->post('good_id');
            $images_str = $request->post("images");//图片
            $images = explode(',',$images_str);
            $data = [
                'store_id' => $store['id'],
                'store_name' => $store['store_name'],
                'thumb' =>  (string)$request->post('thumb'),
                'title' => (string)$request->post('title'),
                'cate_pid' => (int)$request->post('cate_pid'),
                'cate_id' => (int)$request->post('cate_id'),
                'description' => (string)$request->post('description'),
                'content' => (string)$request->post('content'),
                'images' => $images,
                'price' => (int)$request->post('price'),
                'link_name' => (string)$request->post('link_name'),
                'link_phone' => (string)$request->post('link_phone'),
                'link_department' => (string)$request->post('link_department'),
                'link_position' => (string)$request->post('link_position'),
                'storage' => (int)$request->post('storage'),
                'freight' => (int)$request->post('freight'),
                'type' => (int)$request->post('type'),
                'status' => 2,
            ];
            if (!empty($data['link_phone']) && !preg_match('/^((13|14|15|16|17|18)[0-9]\d{8})$/', $data['link_phone'])){
                return $this->writeJson(1,null,'请填写正确的手机号码');
            }

            if(!empty($good_id)){
                $info = Goods::findOrFail($good_id);
                $info->update($data);
                return $this->writeJson(0,null,'修改商品成功');
            }else{
                Goods::create($data);
                return $this->writeJson(0,null,'新增商品成功');
            }

            return $this->writeJson(0, null, '发布成功,请等待平台审核!');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商商品上下架
     * @param url home.user.supplierGoodsStateApi
     * @param goods_id 商品id
     * @param type 1上架 2下架
     * @return bool
     */
    public function supplierGoodsStateApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();//获取当前登录用户
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $goods_id = (int)$request->get('goods_id');
            $type = (int)$request->get('type');
            if(empty($goods_id) || empty($type)) return $this->writeJson(1,null,'参数错误');
            $info = Goods::findOrFail($goods_id);
            if ($info['status'] == 1 || $info['status'] == 4){
                if($type == 1){//上架
                    $info->update(['status' => 1]);
                }elseif($type == 2){//下架
                    $info->update(['status' => 4]);
                }
                return $this->writeJson(0,null,'操作成功');
            }elseif ($info['status'] == 2){
                return $this->writeJson(0,null,'该商品正在审核中');
            }elseif ($info['status'] == 3){
                return $this->writeJson(0,null,'该商品审核失败，请尽快修改商品信息');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值生成订单
     * @param url home.user.supplierRechargeOrderApi
     * @param money 充值金额
     */
    public function supplierRechargeOrderApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('username');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');

            $money = (int)$request->post('money');//充值金额
            //余额日志记录
            $balance = [
                'user_id' => $user_id,
                'user_name' => $userinfo,
                'money' => $money,
                'balance' => '+'.$money,
                'pay_sn' => Buys::makePaySn($user_id),
                'desc' => '余额充值',
                'type' => 3,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
            $balance_id = BalanceLogs::insertGetId($balance);
            return $this->writeJson(0,$balance_id,'生成成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值订单信息
     * @param url home.user.supplierRechargeDataApi
     * @param balance_id 充值订单id
     */
    public function supplierRechargeDataApi(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $balance_id = (int)$request->get('balance_id');//充值订单id
            $lists = BalanceLogs::where(['id'=>$balance_id,'user_id'=>$user_id])->first();

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 供应商余额充值支付
     * @param url home.user.supplierRecharge
     * @param pay_order 充值订单号
     */
    public function supplierRecharge(Request $request){
        try{
            //根据订单信息，生成支付二维码
            $pay_order = $request->post('pay_order');//充值订单号
            $order_info = BalanceLogs::where(array('pay_sn'=>$pay_order))->first();
            $detail = '余额充值支付订单';
            $notify_url = route('home.payment.balanceNotifyUrl');
            $payment = new PaymentController();
            $ret=$payment->wxPayment($pay_order, $order_info['user_id'],$order_info['balance'],'支付订单',$detail,$notify_url);

            if($ret['code'] == 1){
                return $this->writeJson(0,$ret['data']);
            }else{
                return $this->writeJson(1,$ret);
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 对象转换数组
     * @param $e StdClass对象实例
     * @return array|void
     */
    public function objectToArray($e)
    {
        $e = (array)$e;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'resource') return;
            if (gettype($v) == 'object' || gettype($v) == 'array')
                $e[$k] = (array)$this->objectToArray($v);
        }
        return $e;
    }
}